package com.easy.test.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.io.IOUtils;

public class HttpConnector {

	public static final String DEFAULT_CHARSET = "UTF-8";
	public static final String METHOD_POST = "POST";
	public static final String METHOD_GET = "GET";

	public static int connectTimeout = 60000;

	public static int readTimeout = 60000;

	public static final String URL_REDIRECT = "URL_REDIRECT";

	public static String doPostCookie(String url, Map<String, String> params, Map<String, String> cookies)
			throws IOException {
		return doPost(url, params, cookies, DEFAULT_CHARSET, connectTimeout, readTimeout);
	}

	public static String doPostCookie(String url, Map<String, String> params, Map<String, String> cookies,
			int connectTimeout, int readTimeout) throws IOException {
		return doPost(url, params, cookies, DEFAULT_CHARSET, connectTimeout, readTimeout);
	}

	public static String doPost(String url, Map<String, String> params) throws IOException {
		return doPost(url, params, null, DEFAULT_CHARSET, connectTimeout, readTimeout);
	}

	/**
	 * 执行HTTP POST请求。
	 * 
	 * @param url
	 *            请求地址
	 * @param params
	 *            请求参数
	 * @return 响应字符串
	 * @throws IOException
	 */
	public static String doPost(String url, Map<String, String> params, int connectTimeout, int readTimeout)
			throws IOException {
		return doPost(url, params, null, DEFAULT_CHARSET, connectTimeout, readTimeout);
	}

	/**
	 * 执行HTTP POST请求。
	 * 
	 * @param url
	 *            请求地址
	 * @param params
	 *            请求参数
	 * @param charset
	 *            字符集，如UTF-8, GBK, GB2312
	 * @return 响应字符串
	 * @throws IOException
	 */
	public static String doPost(String url, Map<String, String> params, Map<String, String> cookies, String charset,
			int connectTimeout, int readTimeout) throws IOException {
		String ctype = "application/x-www-form-urlencoded;charset=" + charset;
		String query = buildQuery(params, charset);
		byte[] content = {};
		if (query != null) {
			content = query.getBytes(charset);
		}
		return doPost(url, ctype, content, cookies, connectTimeout, readTimeout);
	}

	/**
	 * 执行HTTP POST请求。
	 * 
	 * @param url
	 *            请求地址
	 * @param ctype
	 *            请求类型
	 * @param content
	 *            请求字节数组
	 * @return 响应字符串
	 * @throws IOException
	 */
	public static String doPost(String url, String ctype, byte[] content, Map<String, String> cookies,
			int connectTimeout, int readTimeout) throws IOException {
		HttpURLConnection conn = null;
		OutputStream out = null;
		String rsp = null;
		try {
			try {
				conn = getConnection(new URL(url), cookies, METHOD_POST, ctype);
				conn.setConnectTimeout(connectTimeout);
				conn.setReadTimeout(readTimeout);
			} catch (IOException e) {
				throw e;
			}
			try {

				out = conn.getOutputStream();
				out.write(content);
				rsp = getResponseAsString(conn);
			} catch (IOException e) {
				throw e;
			}

		} finally {
			if (out != null) {
				out.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}

		return rsp;
	}

	/**
	 * 执行带文件上传的HTTP POST请求。
	 * 
	 * @param url
	 *            请求地址
	 * @param textParams
	 *            文本请求参数
	 * @param fileParams
	 *            文件请求参数
	 * @return 响应字符串
	 * @throws IOException
	 */
	public static String doPostFile(String url, Map<String, String> params, Map fileParams, int connectTimeout,
			int readTimeout) throws IOException {
		if (fileParams == null || fileParams.isEmpty()) {
			return doPost(url, params, null, DEFAULT_CHARSET, connectTimeout, readTimeout);
		} else {
			return doPost(url, params, null, fileParams, DEFAULT_CHARSET, connectTimeout, readTimeout);
		}
	}

	public static String doPostXml(String url, Map<String, String> params, Map<String, FileData> fileParams,
			String charset, int connectTimeout, int readTimeout) throws IOException {
		HttpURLConnection conn = null;
		OutputStream out = null;
		String rsp = null;
		try {
			try {
				// String ctype = "multipart/form-data;charset=" + charset +
				// ";boundary=" + boundary;
				conn = getConnection(new URL(url), null, METHOD_POST, "text/xml");

				conn.setConnectTimeout(connectTimeout);
				conn.setReadTimeout(readTimeout);
			} catch (IOException e) {

				throw e;
			}
			out = conn.getOutputStream();
			Set<Entry<String, FileData>> fileEntrySet = fileParams.entrySet();
			for (Entry<String, FileData> fileEntry : fileEntrySet) {
				FileData fileItem = fileEntry.getValue();
				// byte[] fileBytes = getFileEntry(fileEntry.getKey(),
				// fileItem.getFileName(),
				// fileItem.getMimeType(), charset);

				out.write(fileItem.getContent());
			}
			rsp = getResponseAsString(conn);
		} finally {
			if (out != null) {
				out.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}

		return rsp;
	}

	public static HttpResponse doPostAll(String url, Map<String, String> params, Map<String, String> cookies,
			Map<String, FileData> fileParams, String charset, int connectTimeout, int readTimeout) throws IOException {
		String boundary = System.currentTimeMillis() + ""; // 随机分隔线
		HttpURLConnection conn = null;
		OutputStream out = null;
		HttpResponse rsp = null;
		try {
			try {
				if (fileParams != null && fileParams.size() > 0) {
					String ctype = "multipart/form-data;charset=" + charset + ";boundary=" + boundary;
					conn = getConnection(new URL(url), cookies, METHOD_POST, ctype);
				} else {
					String ctype = "application/x-www-form-urlencoded;charset=" + charset;
					conn = getConnection(new URL(url), cookies, METHOD_POST, ctype);
				}

				conn.setConnectTimeout(connectTimeout);
				conn.setReadTimeout(readTimeout);
			} catch (IOException e) {
				// Map<String, String> map=getParamsFromUrl(url);
				// TaobaoLogger.logCommError(e,
				// url,map.get("app_key"),map.get("method"), params);
				throw e;
			}

			try {
				if (fileParams != null && fileParams.size() > 0) {
					out = conn.getOutputStream();

					byte[] entryBoundaryBytes = ("\r\n--" + boundary + "\r\n").getBytes(charset);

					// 组装文本请求参数
					Iterator it = params.entrySet().iterator();
					while (it.hasNext()) {
						Entry textEntry = (Entry) it.next();
						byte[] textBytes = getTextEntry((String) textEntry.getKey(), (String) textEntry.getValue(),
								charset);
						out.write(entryBoundaryBytes);
						out.write(textBytes);
					}

					// 组装文件请求参数
					Set<Entry<String, FileData>> fileEntrySet = fileParams.entrySet();
					for (Entry<String, FileData> fileEntry : fileEntrySet) {
						FileData fileItem = fileEntry.getValue();
						byte[] fileBytes = getFileEntry(fileEntry.getKey(), fileItem.getFileName(), fileItem
								.getMimeType(), charset);
						out.write(entryBoundaryBytes);
						out.write(fileBytes);
						out.write(fileItem.getContent());
					}

					// 添加请求结束标志
					byte[] endBoundaryBytes = ("\r\n--" + boundary + "--\r\n").getBytes(charset);
					out.write(endBoundaryBytes);
					rsp = getResponseAsHttp(conn);
				} else {
					String query = buildQuery(params, charset);
					byte[] content = {};
					if (query != null) {
						content = query.getBytes(charset);
					}
					out = conn.getOutputStream();
					out.write(content);
					rsp = getResponseAsHttp(conn);
				}
			} catch (IOException e) {
				// Map<String, String> map=getParamsFromUrl(url);
				// TaobaoLogger.logCommError(e,
				// conn,map.get("app_key"),map.get("method"), params);
				throw e;
			}

		} finally {
			if (out != null) {
				out.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}

		return rsp;

	}

	public static String doPost(String url, Map<String, String> params, Map<String, String> cookies,
			Map<String, FileData> fileParams, String charset, int connectTimeout, int readTimeout) throws IOException {
		HttpResponse response = doPostAll(url, params, cookies, fileParams, charset, connectTimeout, readTimeout);
		return response.getContent();

	}

	private static HttpURLConnection getConnection(URL url, Map<String, String> cookies, String method, String ctype)
			throws IOException {
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		if (cookies != null) {
			for (String key : cookies.keySet()) {
				String value = cookies.get(key);

				conn.addRequestProperty(key, value);
			}
			String redirect = cookies.get(URL_REDIRECT);
			if ("0".equals(redirect)) {
				conn.setInstanceFollowRedirects(false);
			}
		}
		conn.setRequestMethod(method);
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		// conn.setRequestProperty("Accept",
		// "text/xml,text/javascript,text/html");

		conn.setRequestProperty("User-Agent", "Mozilla/5.0");
		if (ctype != null) {
			conn.setRequestProperty("Content-Type", ctype);
		}
		return conn;
	}

	private static byte[] getTextEntry(String fieldName, String fieldValue, String charset) throws IOException {
		StringBuffer entry = new StringBuffer();
		entry.append("Content-Disposition:form-data;name=\"");
		entry.append(fieldName);
		entry.append("\"\r\nContent-Type:text/plain\r\n\r\n");
		entry.append(fieldValue);
		return entry.toString().getBytes(charset);
	}

	protected static HttpResponse getResponseAsHttp(HttpURLConnection conn) throws IOException {
		String charset = getResponseCharset(conn.getContentType());
		InputStream es = conn.getErrorStream();

		if (es == null) {
			HttpResponse response = new HttpResponse();
			String content = getStreamAsString(conn.getInputStream(), charset);
			response.setContent(content);
			Map<String, String> header = new IdentityHashMap<String, String>();
			Map map = conn.getHeaderFields();
			Iterator it = map.entrySet().iterator();
			while (it.hasNext()) {
				Entry entry = (Entry) it.next();
				List ls = (List) entry.getValue();
				for (int i = 0; i < ls.size(); i++) {
					if (entry.getKey() != null) {
						header.put(new String(entry.getKey().toString()), (String) ls.get(i));
					}
				}
			}

			response.setHeader(header);
			return response;
		} else {
			String msg = getStreamAsString(es, charset);
			if (StringUtil.isEmpty(msg)) {
				throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
			} else {
				throw new IOException(msg);
			}
		}
	}

	protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
		String charset = getResponseCharset(conn.getContentType());
		InputStream es = conn.getErrorStream();

		if (es == null) {
			return getStreamAsString(conn.getInputStream(), charset);
		} else {
			String msg = getStreamAsString(es, charset);
			if (StringUtil.isEmpty(msg)) {
				throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
			} else {
				throw new IOException(msg);
			}
		}
	}

	private static String getResponseCharset(String ctype) {
		String charset = DEFAULT_CHARSET;

		if (!StringUtil.isEmpty(ctype)) {
			String[] params = ctype.split(";");
			for (int i = 0; i < params.length; i++) {
				String param = params[i];
				param = param.trim();
				if (param.startsWith("charset")) {
					String[] pair = param.split("=", 2);
					if (pair.length == 2) {
						if (!StringUtil.isEmpty(pair[1])) {
							charset = pair[1].trim();
						}
					}
					break;
				}
			}
		}

		return charset;
	}

	private static String getStreamAsString(InputStream stream, String charset) throws IOException {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
			StringWriter writer = new StringWriter();

			char[] chars = new char[256];
			int count = 0;
			while ((count = reader.read(chars)) > 0) {
				writer.write(chars, 0, count);
			}

			return writer.toString();
		} finally {
			if (stream != null) {
				stream.close();
			}
		}
	}

	public static String buildQuery(Map<String, String> params, String charset) throws IOException {
		if (params == null || params.isEmpty()) {
			return null;
		}

		StringBuffer query = new StringBuffer();
		// Set entries = params.entrySet();
		boolean hasParam = false;

		Iterator it = params.entrySet().iterator();
		while (it.hasNext()) {
			Entry entry = (Entry) it.next();
			String name = (String) entry.getKey();
			String value = (String) entry.getValue();
			// 忽略参数名或参数值为空的参数
			if (StringUtil.isNotEmpty(name)) {
				if (value == null) {
					value = "";
				}
				if (hasParam) {
					query.append("&");
				} else {
					hasParam = true;
				}

				query.append(name).append("=").append(URLEncoder.encode(value, charset));
			}
		}

		return query.toString();
	}

	private static URL buildGetUrl(String strUrl, String query) throws IOException {
		URL url = new URL(strUrl);
		if (StringUtil.isEmpty(query)) {
			return url;
		}

		if (StringUtil.isEmpty(url.getQuery())) {
			if (strUrl.endsWith("?")) {
				strUrl = strUrl + query;
			} else {
				strUrl = strUrl + "?" + query;
			}
		} else {
			if (strUrl.endsWith("&")) {
				strUrl = strUrl + query;
			} else {
				strUrl = strUrl + "&" + query;
			}
		}

		return new URL(strUrl);
	}

	private static Map<String, String> getParamsFromUrl(String url) {
		Map map = null;
		if (url != null && url.indexOf('?') != -1) {
			map = splitUrlQuery(url.substring(url.indexOf('?') + 1));
		}
		if (map == null) {
			map = new HashMap();
		}
		return map;
	}

	/**
	 * 从URL中提取所有的参数。
	 * 
	 * @param query
	 *            URL地址
	 * @return 参数映射
	 */
	public static Map<String, String> splitUrlQuery(String query) {
		Map result = new HashMap();

		String[] pairs = query.split("&");
		if (pairs != null && pairs.length > 0) {
			for (int i = 0; i < pairs.length; i++) {
				String pair = pairs[i];
				String[] param = pair.split("=", 2);
				if (param != null && param.length == 2) {
					result.put(param[0], param[1]);
				}
			}
		}

		return result;
	}

	/**
	 * 执行HTTP GET请求。
	 * 
	 * @param url
	 *            请求地址
	 * @param params
	 *            请求参数
	 * @return 响应字符串
	 * @throws IOException
	 */
	public static String doGet(String url, Map params) throws IOException {
		return doGet(url, params, null, DEFAULT_CHARSET, connectTimeout, readTimeout);
	}

	public static String doGet(String url, Map<String, String> params, String charset) throws IOException {
		return doGet(url, params, null, charset, connectTimeout, readTimeout);
	}

	public static String doGetCookie(String url, Map<String, String> params, Map<String, String> cookies,
			int connectTimeout, int readTimeout) throws IOException {
		return doGet(url, params, cookies, DEFAULT_CHARSET, connectTimeout, readTimeout);
	}

	public static String doGet(String url, Map<String, String> params, String charset, int connectTimeout,
			int readTimeout) throws IOException {
		return doGet(url, params, null, DEFAULT_CHARSET, connectTimeout, readTimeout);
	}

	/**
	 * 执行HTTP GET请求。
	 * 
	 * @param url
	 *            请求地址
	 * @param params
	 *            请求参数
	 * @param charset
	 *            字符集，如UTF-8, GBK, GB2312
	 * @return 响应字符串
	 * @throws IOException
	 */
	public static String doGet(String url, Map<String, String> params, Map<String, String> cookies, String charset,
			int connectTimeout, int readTimeout) throws IOException {
		HttpURLConnection conn = null;
		String rsp = null;

		try {
			String ctype = "application/x-www-form-urlencoded;charset=" + charset;
			String query = buildQuery(params, charset);
			try {
				conn = getConnection(buildGetUrl(url, query), cookies, METHOD_GET, ctype);
				conn.setConnectTimeout(connectTimeout);
				conn.setReadTimeout(readTimeout);
			} catch (IOException e) {
				// Map<String, String> map=getParamsFromUrl(url);
				// TaobaoLogger.logCommError(e,
				// url,map.get("app_key"),map.get("method"), params);
				throw e;
			}

			try {
				rsp = getResponseAsString(conn);
			} catch (IOException e) {
				// Map<String, String> map=getParamsFromUrl(url);
				// TaobaoLogger.logCommError(e,
				// conn,map.get("app_key"),map.get("method"), params);
				throw e;
			}

		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}

		return rsp;
	}

	private static byte[] getFileEntry(String fieldName, String fileName, String mimeType, String charset)
			throws IOException {
		StringBuilder entry = new StringBuilder();
		entry.append("Content-Disposition:form-data;name=\"");
		entry.append(fieldName);
		entry.append("\";filename=\"");
		entry.append(fileName);
		entry.append("\"\r\nContent-Type:");
		entry.append(mimeType);
		entry.append("\r\n\r\n");
		return entry.toString().getBytes(charset);
	}

	public static byte[] doDownLoadFile(String url, String ctype, byte[] content, int connectTimeout, int readTimeout)
			throws IOException {
		HttpURLConnection conn = null;
		OutputStream out = null;
		byte[] rsp = null;
		try {
			try {
				conn = getConnection(new URL(url), null, METHOD_POST, ctype);
				conn.setConnectTimeout(connectTimeout);
				conn.setReadTimeout(readTimeout);
			} catch (IOException e) {
				throw e;
			}
			try {
				out = conn.getOutputStream();
				out.write(content);
				rsp = getResponseAsFile(conn);
			} catch (IOException e) {
				throw e;
			}

		} finally {
			if (out != null) {
				out.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}

		return rsp;
	}

	public static byte[] doDownLoadFile(String url, Map<String, String> params, int connectTimeout, int readTimeout)
			throws IOException {

		return doDownLoadFile(url, params, DEFAULT_CHARSET, connectTimeout, readTimeout);

	}

	public static byte[] doDownLoadFile(String url, Map<String, String> params, String charset, int connectTimeout,
			int readTimeout) throws IOException {
		String ctype = "application/x-www-form-urlencoded;charset=" + charset;
		String query = buildQuery(params, charset);
		byte[] content = {};
		if (query != null) {
			content = query.getBytes(charset);
		}
		return doDownLoadFile(url, ctype, content, connectTimeout, readTimeout);
	}

	protected static byte[] getResponseAsFile(HttpURLConnection conn) throws IOException {
		String charset = getResponseCharset(conn.getContentType());
		InputStream es = conn.getErrorStream();
		if (es == null) {

			return IOUtils.toByteArray(conn.getInputStream());
		} else {
			String msg = getStreamAsString(es, charset);
			if (StringUtil.isEmpty(msg)) {
				throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
			} else {
				throw new IOException(msg);
			}
		}
	}

	public static void main(String args[]) {
		/*try {
			byte[] b = HttpConnector.doDownLoadFile("http://msict.nbport.com.cn/webapp/jsp/randcode1.jsp",
					new HashMap(), connectTimeout, readTimeout);
			File f = new File("d://2.jpg");
			if (!f.exists()) {
				f.createNewFile();
			}
			FileUtils.writeByteArrayToFile(f, b);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

}
